<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/8
 * Time: 下午1:34
 */
class ServerController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('服务器管理');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$filter->addFilter('server_name', 'input', '服务器名称')
            ->addFilter('show_status', 'select', '表现状态', array('value' => '', 'options' => array('' => '所有') + ServerService::showStatusList()));
		$filter->addSpecial(true);
		
		$filter->addFilter('begin', 'input', '开服时间', array('date' => 'day'))
		->addFilter('end', 'input', '至', array('date' => 'day'));
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$params['is_qu'] = 0;
		$header = ServerService::indexHeader();
		if(PubFunc::isAjax()){
			$result = ServerService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=server/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'server/index',
					'length'=>50,
					'page'=>$page,
					'fix' => array(
							'col' => 3
					)
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
	
		}
	}
	
	/**
	 * 简易版列表
	 */
	public function qindex(){
		$this->setTitle('服务器管理');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$filter->addFilter('server_name', 'input', '服务器名称')
		->addFilter('show_status', 'select', '表现状态', array('value' => '', 'options' => array('' => '所有') + ServerService::showStatusList()));
		$filter->addSpecial(true);
		
		$filter->addFilter('begin', 'input', '开服时间', array('date' => 'day'))
		->addFilter('end', 'input', '至', array('date' => 'day'));
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = ServerService::qindexHeader();
		$params['is_qu'] = 1;
		if(PubFunc::isAjax()){
			$result = ServerService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=server/qindex", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'server/index',
					'length'=>50,
					'page'=>$page,
					'fix' => array(
							'col' => 3
					)
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
	
		}
	}


    public function add(){
        $this->setTitle('新建服务器');

        $this->out['statusList'] = ServerService::statusList();
        $this->out['serverTypeList'] = ServerService::serverTypeList();
        
        $specialList = SpecialServerService::specialList();
        $specialList = array(''=>'请选择') + $specialList;
        $this->out['specialList'] = $specialList;
        
        $checkFlagList = ServerService::checkFlagList();
        $this->out['checkFlagList'] = $checkFlagList;
        
        $hefuFlagList = ServerService::hefuFlagList();
        $this->out['hefuFlagList'] = $hefuFlagList;
        
        $showStatusList = ServerService::showStatusList();
        $this->out['showStatusList'] = $showStatusList;
    }

    public function doAdd(){
    	//判断所选的专服是否设置了同步路径
    	$specialinfo = SpecialServerService::get($this->R('special_id'));
    	if($specialinfo['api_synchro_url'] == ''){
    		$this->showMessage('所选专服未设置同步URL');
    	}else{
    		$count = ServerService::repeat(0, 'develop_id', $this->R('develop_id'),$this->R('g'));
    		$count_num = ServerService::repeat(0, 'server_num', $this->R('server_num'),$this->R('g'));
    		$count_name = ServerService::repeat(0, 'name', $this->R('name'),$this->R('g'));
    		if($count > 0){
    			$this->showMessage('开发ID已经存在', PubFunc::buildUrl("/oss/?r=server/index"));
    		}else if($count_name >0){
    			//$this->showMessage('游戏结点名已经存在', PubFunc::buildUrl("/oss/?r=server/index"));
    		}else{
    			$api_synchro_url = $specialinfo['api_synchro_url'];
    			$result = ServerService::doAdd($_POST,$api_synchro_url);
    			if($result['state'] !== false){
    				$this->showMessage('新增成功', PubFunc::buildUrl("/oss/?r=server/index"));
    			}else{
    				$this->showMessage('新增失败'.$result['msg'],PubFunc::buildUrl("/oss/?r=server/index"));
    			}
    		}
    	}
    }

    public function edit(){
        $id = $this->R('id');
        $info = ServerService::get($id);

        if(empty($info)) $this->showMessage('找不到该信息');
        
        //查询服务器对应的专服
        $modSpecialServicer = new ModSpecialServicer();
        $obj = $modSpecialServicer->findByAttributes(array('server_id' => $id));
        $info['special_id'] = $obj->special_id? $obj->special_id : 0;
        //查询服务器对应的专服
        
        //根据专服id获取渠道
        $channelList = ChannelService::alreadyChannel($id);
        $channes = '';
        foreach ($channelList as $key=>$val){
        	$channes .= ','.$val;
        }
        $channes = substr($channes, 1);
        $this->out['channes'] = $channes;
        //根据专服id获取渠道

        $this->setTitle('编辑服务器', $info['server_name']);

        $this->out['info'] = $info;
        $this->out['statusList'] = ServerService::statusList();
        $this->out['serverTypeList'] = ServerService::serverTypeList();
        
        $specialList = SpecialServerService::specialList();
        $specialList = array(''=>'请选择') + $specialList;
        $this->out['specialList'] = $specialList;
        
        $backUrl = PubFunc::buildBackUrl("/oss/?r=server/index");  //返回路径
        $this->out['backUrl'] = $backUrl;
        
        $page = $this->R('page');
        $this->out['page'] = $page;
        
        $checkFlagList = ServerService::checkFlagList();
        $this->out['checkFlagList'] = $checkFlagList;
        
        $hefuFlagList = ServerService::hefuFlagList();
        $this->out['hefuFlagList'] = $hefuFlagList;
        
        $showStatusList = ServerService::showStatusList();
        $this->out['showStatusList'] = $showStatusList;
    }

    public function doEdit(){
    	$specialinfo = SpecialServerService::get($this->R('special_id'));
    	if($specialinfo['api_synchro_url'] == '') {
    		$this->showMessage('所选专服未设置同步URL');
    	}else{   		
    		$id = $this->R('id');
    		$count = ServerService::repeat($this->R('id'), 'develop_id', $this->R('develop_id'),$this->R('g'));
    		$count_num = ServerService::repeat($this->R('id'), 'server_num', $this->R('server_num'),$this->R('g'));
    		$count_name = ServerService::repeat($this->R('id'), 'name', $this->R('name'),$this->R('g'));
    		if($count > 0){
    			$this->showMessage('开发ID已经存在', PubFunc::buildBackUrl("/oss/?r=server/index"));
    		}else if($count_name >0){
    			//$this->showMessage('游戏结点名已经存在', PubFunc::buildBackUrl("/oss/?r=server/index"));
    		}else{
    			$api_synchro_url = $specialinfo['api_synchro_url'];
    			$result = ServerService::doEdit($id, $_POST,$api_synchro_url);
    		
    			if($result['state'] !== false){
    				$this->showMessage('编辑成功', PubFunc::buildBackUrl("/oss/?r=server/index"));
    			}else{
    				$this->showMessage('编辑失败:'.$result['msg']);
    			}
    		}
    	}
    }
    
    /**
     * 简易版编辑
     */
    public function qedit(){
    	$id = $this->R('id');
    	$info = ServerService::get($id);
    
    	if(empty($info)) $this->showMessage('找不到该信息');
    
    	//查询服务器对应的专服
    	$modSpecialServicer = new ModSpecialServicer();
    	$obj = $modSpecialServicer->findByAttributes(array('server_id' => $id));
    	$info['special_id'] = $obj->special_id? $obj->special_id : 0;
    	//查询服务器对应的专服
    
    	//根据专服id获取渠道
    	$channelList = ChannelService::alreadyChannel($id);
    	$channes = '';
    	foreach ($channelList as $key=>$val){
    		$channes .= ','.$val;
    	}
    	$channes = substr($channes, 1);
    	$this->out['channes'] = $channes;
    	//根据专服id获取渠道
    
    	$this->setTitle('编辑服务器', $info['server_name']);
    
    	$this->out['info'] = $info;
    	$this->out['statusList'] = ServerService::statusList();
    
    	$specialList = SpecialServerService::specialList();
    	$specialList = array(''=>'请选择') + $specialList;
    	$this->out['specialList'] = $specialList;
    
    	$backUrl = PubFunc::buildBackUrl("/oss/?r=server/qindex");  //返回路径
    	$this->out['backUrl'] = $backUrl;
    
    	$page = $this->R('page');
    	$this->out['page'] = $page;
    
    	$checkFlagList = ServerService::checkFlagList();
    	$this->out['checkFlagList'] = $checkFlagList;
    
    	$showStatusList = ServerService::showStatusList();
    	$this->out['showStatusList'] = $showStatusList;
    }
    
    public function qdoEdit(){
    	$specialinfo = SpecialServerService::get($this->R('special_id'));
    	if($specialinfo['api_synchro_url'] == '') {
    		$this->showMessage('所选专服未设置同步URL');
    	}else{
    		$id = $this->R('id');
    		$count = ServerService::repeat($this->R('id'), 'develop_id', $this->R('develop_id'),$this->R('g'));
    		$count_num = ServerService::repeat($this->R('id'), 'server_num', $this->R('server_num'),$this->R('g'));
    		$count_name = ServerService::repeat($this->R('id'), 'name', $this->R('name'),$this->R('g'));
    		if($count > 0){
    			$this->showMessage('开发ID已经存在', PubFunc::buildBackUrl("/oss/?r=server/qindex"));
    		}else if($count_name >0){
    			//$this->showMessage('游戏结点名已经存在', PubFunc::buildBackUrl("/oss/?r=server/qindex"));
    		}else{
    			$api_synchro_url = $specialinfo['api_synchro_url'];
    			$result = ServerService::qdoEdit($id, $_POST,$api_synchro_url);
    
    			if($result['state'] !== false){
    				$this->showMessage('编辑成功', PubFunc::buildBackUrl("/oss/?r=server/qindex"));
    			}else{
    				$this->showMessage('编辑失败:'.$result['msg']);
    			}
    		}
    	}
    }
    

    public function delete(){
        $id = $this->R('id');

        $result = ServerService::delete($id);

        if($result !== false){
            $this->showMessage('删除成功', PubFunc::buildBackUrl("/oss/?r=server/index"));
        }else{
            $this->showMessage('删除失败');
        }
    }
    
    
    /**
     * 开通渠道
     */
    public function setChannel(){
    	$this->setTitle('开通渠道');
    	
    	$channelList = PubServer::getPacketOptions($game, 'id');
    	$this->out['channelList'] = $channelList;
    	
    	$id = $this->R('id');
    	$this->out['id'] = $id;	
    	
    	//查询已经设置的渠道
    	$already = ServerService::alreadyChannel($id);
    	$this->out['already'] = $already;
    }
    
    public function doChannel(){
    	//先删除原来的，再重新插入新的
    	ServerService::batchDelete($_POST['server_id']);
    	
    	$result = ServerService::doChannel($_POST);
    	if($result !== false){
    		$this->showMessage('设置成功', PubFunc::buildUrl("/oss/?r=server/index"));
    	}else{
    		$this->showMessage('设置失败');
    	}
    }
    
    public function stopServer(){
    	if(PubFunc::isAjax()){
    		$server = PubRequest::R('server');
    		
    		$this->out = array(
    				'status' => 1,
    				'msg' => '成功',
    				'data'    => ''
    		);
    		$this->outType = 'json';
    	}else{
    		$this->setTitle('服务器停更');
    		 
    		$filter = new PubFilter();
    		$filter->addFilter('status', 'select', '服务器类型', array('value' => '', 'options' => array('' => 'ALL') + ServerService::statusList()));
    		$params = $filter->get();
    		$this->out['filter'] = $filter->show();
    		 
    		$data = ServerService::getSpecialServer($params['status']);
    		$this->out['data'] = $data;
    		
    		$url = PubFunc::buildUrl("/auth/?r=user/channel");
    		$this->out['url'] = $url;
    	}
    	
    	
    }
    
    /**
     * 批量修改状态
     */
    public function updateStatus(){
    	set_time_limit (0);
    	$ids = PubRequest::R('ids');
    	$status = PubRequest::R('status');
    	
    	if($ids == ''){
    		$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
    	}else{
    		$out = ServerService::updateStatus($ids,$status);
    	}
    	
    	$this->out = $out;
    	$this->outType = 'json';
    }
    
    
    /**
     * 定时清档
     */
    public function fixedTime(){
    	$this->setTitle('服务器定时清档');
    	
    	$filter = new PubFilter();
    	$grid   = new PubGrid();
    	
    	$filter->addFilter('server_name', 'input', '服务器名称')
    	->addFilter('show_status', 'select', '表现状态', array('value' => '', 'options' => array('' => '所有') + ServerService::showStatusList()));
    	$filter->addSpecial(true);
    	
    	$filter->addFilter('begin', 'input', '开服时间', array('date' => 'day'))
    	->addFilter('end', 'input', '至', array('date' => 'day'));
    	
    	$params = $filter->get();
    	$page = $this->R('page', '1');
    	$m = $this->R('m');
    	$params['m'] = $m;
    	$header = ServerService::fixedTimeHeader();
    	if(PubFunc::isAjax()){
    		$result = ServerService::fixedTimeData($params);
    		$this->out = array(
    				'count'     => $result['count'],
    				'data'      => $result['data'],
    				'result'    => true
    		);
    		$this->outType = 'json';
    	}else{
    		$order  = 'create_time';
    		$url = PubFunc::ajaxUrl("?r=server/fixedTime", $params);
    		$grid->addGrid($header, $url, array(
    				'name'=>'数据',
    				'order'=>array($order, 'desc'),
    				'type'=>'server/fixedTime',
    				'length'=>50,
    				'page'=>$page,
    				'fix' => array(
    						'col' => 2
    				)
    		));
    	
    		$this->out['filter'] = $filter->show();
    		$this->out['grid'] = $grid->show();
    	
    	}
    }
    
    public function fixedTimeEdit(){
    	$id = $this->R('id');
    	$info = ServerService::get($id);
    
    	if(empty($info)) $this->showMessage('找不到该信息');

    	if($info['open_time'] > time()) $this->showMessage('该服务器已经开服，不能进行清档');
    	
    	if($info['clear_time'] == 0){
    		$info['clear_time'] = '';
    	}else{
    		$info['clear_time']  = date('Y-m-d H:i:s', $info['clear_time']);
    	}
    
    	//查询服务器对应的专服
    	$modSpecialServicer = new ModSpecialServicer();
    	$obj = $modSpecialServicer->findByAttributes(array('server_id' => $id));
    	$info['special_id'] = $obj->special_id? $obj->special_id : 0;
    	//查询服务器对应的专服
    	
    	$channelName = ServerService::getChannel($info['id']);
    	$this->out['channes'] = $channelName;
    
    	$this->setTitle('修改清档时间', $info['server_name']);
    
    	$this->out['info'] = $info;
   
    	$specialList = SpecialServerService::get($info['special_id']);
    	$this->out['specialName'] = $specialList['name'];
    
    	$backUrl = PubFunc::buildBackUrl("/oss/?r=server/fixedTime");  //返回路径
    	$this->out['backUrl'] = $backUrl;
    
    	$page = $this->R('page');
    	$this->out['page'] = $page;
    
    	$showStatusList = ServerService::showStatusList();
    	$this->out['showStatus'] = $showStatusList[$info['show_status']];
    }
    
    public function fixedTimeDoEdit(){
    	$id = $this->R('id');
    	$result = ServerService::fixedTimeDoEdit($id, $_POST);
    	if($result['state'] !== false){
    		$this->showMessage('编辑成功', PubFunc::buildBackUrl("/oss/?r=server/fixedTime"));
    	}else{
    		$this->showMessage('编辑失败:'.$result['msg']);
    	}
    }
    
    
    

}